#include "isr.h"
#include "idt.h"
#include "i386.h"
#include "klib.h"

#define INT_IDT(i, isr) {\
	idt[i].offset_0_15 = (unsigned short) ((unsigned int)(isr) & (unsigned int) 0xFFFF);\
	idt[i].segsel = (unsigned short) 0x0008;\
	idt[i].attr = (unsigned short) 0x8E00;\
	idt[i].offset_16_31 = (unsigned short) ((unsigned int)(isr) >> 16 & (unsigned int) 0xFFFF);\
}

#define INT_IDT_USER(i, isr) {\
	idt[i].offset_0_15 = (unsigned short) ((unsigned int)(isr) & (unsigned int) 0xFFFF);\
	idt[i].segsel = (unsigned short) 0x0008;\
	idt[i].attr = (unsigned short) 0xEE00;\
	idt[i].offset_16_31 = (unsigned short) ((unsigned int)(isr) >> 16 & (unsigned int) 0xFFFF);\
}

void inicializar_idt() {
	INT_IDT(0,&_isr0);
	INT_IDT(1,&_isr1);
	INT_IDT(2,&_isr2);
	INT_IDT(3,&_isr3);
	INT_IDT(4,&_isr4);
	INT_IDT(5,&_isr5);
	INT_IDT(6,&_isr6);
	INT_IDT(7,&_isr7);
	INT_IDT(8,&_isr8);
	INT_IDT(9,&_isr9);
	INT_IDT(10,&_isr10);
	INT_IDT(11,&_isr11);
	INT_IDT(12,&_isr12);
	INT_IDT(13,&_isr13);
	INT_IDT(14,&_isr14);
	INT_IDT(15,&_isr15);
	INT_IDT(16,&_isr16);
	INT_IDT(17,&_isr17);
	INT_IDT(18,&_isr18);
	INT_IDT(19,&_isr19);
	INT_IDT(32,&_isr_clock);
	INT_IDT(33,&_isr_teclado);
	INT_IDT_USER(88,&_isr88);
	INT_IDT_USER(89,&_isr89);
}

idt_entry idt[255] = {};

char* excepciones[20]= {"Divide Error","RESERVED","NMI Interrupt","Breakpoint","Overflow","BOUND Range Exceeded","Invalid Opcode","Device Not Available","Double Fault","Compressor Segment Overrun","Invalid TSS","Segment Not Present","Stack-Segment Fault","General Protection Fault","Page Fault","RESERVED","FPU Floating Point Error","Alignment Check","Machine Check","SIMD Floating Point Exception"};

char* reg_generales[]={"EAX     ","EBX     ","ECX     ","EDX     ","ESI     ","EDI     ","EBP     ","ESP     "}; //len = 8

char* reg_control[]={"CR0     ","CR2     ","CR3     ","CR4     "}; //len = 8

char* reg_eflags="EFLAGS  "; //len = 8

char* reg_segmento[]={"CS      ","DS      ","ES      ","FS      ","GS      ","SS      "}; //len = 8

char* stack="Stack:     "; //len = 11

idt_descriptor IDT_DESC = {sizeof(idt)-1, (unsigned int)&idt};

/*
IDT
	unsigned short offset_0_15;
	unsigned short segsel;
	unsigned short attr;
	* P:1 = 1b
	* DPL:2 = 00
	* S:1 = 0b
	* D:1 = 1b (32 bits)
	* T:3 = 110b (Tipo = interrupt)
	* L:8 = 00000000b (Low_pos = 0)
	unsigned short offset_16_31;
*/
